package com.alex.service.ipml;

import com.alex.entity.Alexhqline;
import com.alex.entity.ShsqWater;
import com.alex.mapper.ShQfqWaterMapper;
import com.alex.service.NoticeService;
import com.alex.service.ShQfqWaterService;
import com.alex.utils.AlexUtil;
import com.alex.utils.PageData;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ShQfqWaterServiceImpl implements ShQfqWaterService {

    @Autowired
    private ShQfqWaterMapper shQfqWaterMapper;

    @Autowired
    private NoticeService noticeService;

    @Override
    public PageData<ShsqWater> getShsqWaters(String tabname,Integer pageSize,Integer currentPage) {
        Integer totalRows = shQfqWaterMapper.getShsqWaterstotal(tabname);
        PageData<ShsqWater> res = new PageData<ShsqWater>(currentPage,pageSize,totalRows);
        List<String> stcds = shQfqWaterMapper.getstcds();
        List<ShsqWater> qe = new ArrayList<>();
        for(String stcd : stcds){
            ShsqWater st = shQfqWaterMapper.getShsqWaters(tabname,stcd);
            if (st != null){
                if ("0066668806".equals(stcd) || "0066668805".equals(stcd)){
                    if ("0066668806".equals(stcd)){
                        st.setQ(gettwoq(getq(st.getZ())));
                    }
                    qe.add(0, st);
                }else {
                    qe.add(st);
                }
            }
        }
        res.setRows(qe);
        return res;
    }

    @Override
    public PageData<ShsqWater> getDevices(String tabname, Integer pageSize, Integer currentPage) {
        Integer totalRows = shQfqWaterMapper.getDevicestotal();
        PageData<ShsqWater> res = new PageData<ShsqWater>(currentPage,pageSize,totalRows);
        List<String> stcds = shQfqWaterMapper.getstcds();
        List<ShsqWater> qe = new ArrayList<>();
        for(String stcd : stcds){
            ShsqWater st = shQfqWaterMapper.getShsqWaters(tabname,stcd);
            if (st != null){
                if ("0066668806".equals(stcd) || "0066668805".equals(stcd)){
                    if ("0066668806".equals(stcd)){
                        st.setQ(gettwoq(getq(st.getZ())));
                    }
                    qe.add(0, st);
                }else {
                    qe.add(st);
                }
            }
        }
        res.setRows(qe);
        return res;
    }

    @Override
    public Map<String, Object> getOnlines() {
        Map<String, Object> res = new HashMap<>();
        Integer ons = shQfqWaterMapper.getOnlines();
        Integer total = shQfqWaterMapper.getTotals();
        res.put("totaldev",total);
        res.put("online",ons);
        return res;
    }

    @Override
    public List<ShsqWater> getNowData() {
        List<String> stcds = shQfqWaterMapper.getstcds();
        List<ShsqWater> qe = new ArrayList<>();
        for (String stcd : stcds){
            ShsqWater st = shQfqWaterMapper.getDevices(AlexUtil.getTableName(new Date()),stcd);
            if (st != null){
                if ("0066668806".equals(stcd)){
                    st.setQ(gettwoq(getq(st.getZ())));
                }
                qe.add(st);
            }
        }
        return qe;
    }

    public Double getq(Double z){
        Map<String,Object> map = new HashMap<>();
        map = noticeService.getmapq(z);
        Alexhqline up = (Alexhqline) map.get("up");
        Alexhqline down;
        if (up.getZ() == 770){
            down = (Alexhqline) map.get("up");
        }else {
            down = (Alexhqline) map.get("down");
        }
        if (down.getZ() - up.getZ() == 0.0){
            return up.getQ();
        }else {
            Double a = (down.getQ() - up.getQ()) / (down.getZ() - up.getZ());
            Double k = down.getQ() - (a * down.getZ());
            Double resq = a * z + k;
            return gettwoq(resq);
        }
    }

    public Double gettwoq(Double q){
        BigDecimal bg = new BigDecimal(q);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }
}
